package cn.siriusbot.siriuspro.admin.dao;

import cn.siriusbot.siriuspro.admin.entity.AppConfig;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface AppConfigMapper extends BaseMapper<AppConfig> {

    @Select("select * from `app_config` where `plugins_id` = #{plugins_id} and `database` = #{database} and `node` = #{node} and `config` = #{config}")
    AppConfig queryOne(@Param("plugins_id") int appId, @Param("database") String database, @Param("node") String node, @Param("config") String config);

    @Select("select * from `app_config` where `plugins_id` = #{plugins_id} and `database` = #{database}")
    List<AppConfig> getNodeList(@Param("plugins_id") int appId, @Param("database") String database);

    @Select("select * from `app_config` where `plugins_id` = #{plugins_id} and `database` = #{database} and `node` = #{node}")
    List<AppConfig> getConfigList(@Param("plugins_id") int appId, @Param("database") String database, @Param("node") String node);

    @Select("select * from `app_config` where `plugins_id` = #{plugins_id}")
    List<AppConfig> getDatabaseList(@Param("plugins_id") int appId);

    /**
     * 删除数据库
     *
     * @param database 数据库名(->易语言中的文件名)
     */
    @Update("delete from `app_config` where `plugins_id` = #{plugins_id} and `database` = #{database}")
    void delDatabase(@Param("plugins_id") int appId, @Param("database") String database);

    /**
     * 删除节名
     *
     * @param database 数据库名(->易语言中的文件名)
     * @param node     节名
     */
    @Update("delete from `app_config` where `plugins_id` = #{plugins_id} and `database` = #{database} and `node` = #{node}")
    void delNode(@Param("plugins_id") int appId, @Param("database") String database, @Param("node") String node);

    /**
     * 删除配置项
     *
     * @param database 数据库名(->易语言中的文件名)
     * @param node     节名
     * @param config   配置项名
     */
    @Update("delete from `app_config` where `plugins_id` = #{plugins_id} and `database` = #{database} and `node` = #{node} and `config` = #{config}")
    void delConfig(@Param("plugins_id") int appId, @Param("database") String database, @Param("node") String node, @Param("config") String config);
}
